<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Facades\Utils\QiniuFileManager;

class Order extends Model
{

    const STATUS_COMPLETE = 1;

    const STATUS_FAIL = 0;

    public static $STATUS = [
        self::STATUS_COMPLETE => "完成",
        self::STATUS_FAIL => "未完成"
    ];

    protected $table = 'orders';

    protected $fillable = [
    	'c_name',
    	'c_number',
    	'c_picture',
    	'c_integration',
    	'status',
    	'order_id',
    	'address',
    	'phone',
    	'collection_name',
    	'customer_id'
    ];

    protected $appends = [
        'status_desc',
        'c_picture_url',
        'c_picture_arr'
    ];

    public function getStatusDescAttribute(){
        if(array_key_exists('status',$this->attributes)){
            return $this->attributes['status_desc'] = self::$STATUS[$this->attributes['status']];
        }
    }

    public function getCPictureUrlAttribute(){
      if(array_key_exists('c_picture',$this->attributes)){
        $tmpCPictureArr = json_decode($this->attributes['c_picture']);
        $tempArray = [];
        for($i = 0;$i<count($tmpCPictureArr);$i++){
          $tempArray[$i] = QiniuFileManager::getFileUrl($tmpCPictureArr[$i],'private');
        }
        return $tempArray;
      }
    }

    public function getCPictureArrAttribute(){
      if(array_key_exists('c_picture',$this->attributes)){
        return json_decode($this->attributes['c_picture']);
      }
    }

}
